/**
 * 提现,账户付款给对方银行 交易操作
 */
package cn.agiledata.bank.transaction.service;

import java.util.Date;

import org.apache.commons.beanutils.PropertyUtils;

import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.service.email.GainMoneyEmail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.hibernateBean.EsPrePayTrans;
import cn.agiledata.bank.hibernateBean.Transaction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class EsPrePayTransRemit extends EsPrePayTransFactoryAbstract {
	private SysTemplate sysTemplate;

	public void setSysTemplate(SysTemplate sysTemplate) {
		this.sysTemplate = sysTemplate;
	}
    
    private TransactionTemplate getTransactionTemplate(){
        return (TransactionTemplate)IPBAppFactory.getObj(ForeDict.TRANSACTION_TEMPLATE);
    }

	public synchronized void saveTransaction(EsPrePayTrans esPrePayTrans) {

		// 获得具体收费模式
		if (esPrePayTrans.isGiveBackPayFee() == Constant.GIVEBACK_PAYFEE_Y) {
			esPrePayTrans.setAmount(Double.valueOf(String.valueOf(esPrePayTrans
					.getAmount().doubleValue()
					+ esPrePayTrans.getDebitFee().doubleValue())));
			esPrePayTrans.setDebitFee(new Double(0));
		}
		// 如果是未确认状态只保存一条交易记录
		getHibernateTemplate().save(esPrePayTrans);

		getHibernateTemplate().update(esPrePayTrans.getDebit());

	}
	
	public void updateTransaction(EsPrePayTrans esPrePayTrans) {
		
	}

	public void esPrePayTransToTransaction(EsPrePayTrans esPrePayTrans) {

		// 拷贝EsPrePayTrans对象到trans
	    Transaction trans = null;
        String transId = esPrePayTrans.getTransId();
        if (transId != null && transId.length() > 0) {
            trans = getTransactionTemplate().getTransactionByTranId(transId);
        }
        if (trans == null) {
            trans = new Transaction();
        }
		try {
			PropertyUtils.copyProperties(trans, esPrePayTrans);
		} catch (Exception e) {

			e.printStackTrace();
		}
		
		esPrePayTrans.setTransStatus(Constant.ES_TRANS_STATUS_FINISH);
		getHibernateTemplate().saveOrUpdate(esPrePayTrans);

		// 修正trans状态
		trans.setId(null);
		trans.setTransStatus(Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
		trans.setAuditTrans("0");
		trans.setEndDate(new Date());
		trans.setUpdateTime(new Date());
		

		GetTransaction getTran = new GetTransactionImpl();
		//退款交易的新增处理逻辑:
        //当交易类型为退款.但bank_seq为空的时候.说明该条交易之前为账户间交易. 直接走账户间交易流程.
        //这里通过临时改变actiontype来实现
		String actionType = String.valueOf(esPrePayTrans
        		.getActionType().getId());
		if (Constant.ACTION_TYPE_R.equals(actionType) && esPrePayTrans.getBankSeq() == null){
        	actionType = Constant.ACTION_TYPE_C;
        }
        //end
		TransactionFactory tt = getTran.getTransaction(actionType);

		tt.saveTransaction(trans);
		if(actionType == Constant.ACTION_TYPE_R) {
		  TransactionTemplate template = (TransactionTemplate) IPBAppFactory.getObj("transactionTemplate");
		  GainMoneyEmail mail = (GainMoneyEmail) IPBAppFactory .getObj("gainMoneyMail");
		  if (template.ifSendMail(trans.getDebit().getAccount(), Constant.GAINMONEY)) {
			  if (trans.getActionType().getId().toString().equals(Constant.ACTION_TYPE_G)) {
				  mail.setEmailTitle("您的账户直付通知");
				  mail.setContentTitle("直付");
			  } else if(trans.getActionType().getId().toString().equals(Constant.ACTION_TYPE_R)) {
				  mail.setEmailTitle("您的账户退款通知");
				  mail.setContentTitle("退款");
			  } else {
				  mail.setEmailTitle("您的账户提现通知");
				  mail.setContentTitle("提现");
			  }
			  mail.sendMail(trans, null);
		  }
		  if (template.ifSendMobile(trans.getDebit().getAccount(), Constant.GAINMONEY)) {
			  if (trans.getActionType().getId().toString().equals(Constant.ACTION_TYPE_G)) {
				  mail.setContentTitle("账户直付");
				  mail.setSmsContext("sms-drawToOther.vm");
			  } else {
				  mail.setEmailTitle("您的账户提现通知");
				  mail.setContentTitle("提现");
			  }
			  mail.sendSms(trans, null);
		  }
		}
	}
}
